package com.starbaba.channelmanager.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.base.common.database.BaseDao;
import com.base.common.exception.AppException;
import com.starbaba.channelmanager.model.Channel;
import com.starbaba.channelmanager.service.ChannelService;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.model.dictionary.Constant;
import com.starbaba.stat.model.StatActivitySummaryModel;

@SuppressWarnings("deprecation")
@Component("tChannelServiceImpl")
public class ChannelServiceImpl implements ChannelService {
	
	public  String getExportString(List<Channel> list) {
		StringBuilder builder = new StringBuilder();
		builder.append("ID,渠道,渠道码,产品ID,产品名称\r\n");
		for (Channel vo : list) {
			builder.append(vo.getId()).append(","); 
			builder.append(vo.getChannel_name()).append(","); 
			builder.append(vo.getChannel_code()).append(","); 
			builder.append(vo.getPrdid()).append(",");
			builder.append(vo.getKindname()).append(","); 
			builder.append("\r\n");
		}
		return builder.toString();
	}
	
	
    /**
	 * 查询渠道信息的方法
	 */
	@Override
	public List<Channel> queryChannelVo(Map<String, Object> paramsMap, Integer iDisplayStart, Integer iDisplayLength,
			@SuppressWarnings("rawtypes") PageResponse pageResponse) {
		List<Channel> beanList=null;
        BaseDao dao=new BaseDao(Constant.POOL_LIFE);
		List<Object> paramsList=new ArrayList<Object>();
		StringBuffer sb=new StringBuffer();
		sb.append("select id,channel_name,channel_code,prdid,(SELECT name FROM comm_product_prdid WHERE prdid = comm_product_channel.prdid) as kindname  from comm_product_channel where 1=1");
		String name=(String) paramsMap.get("channel_name");
		String prdid = (String) paramsMap.get("prdid");
		String channel_code = (String)paramsMap.get("channel_code");
		if(name!=null && name!=""){
			sb.append(" and channel_name like '"+name+"%' ");
//			paramsList.add("'"+name+"%'");
		}
		if(! StringUtils.isEmpty(prdid)){
			sb.append(" and prdid = ?");
			paramsList.add(prdid);
		}
		
		if(! StringUtils.isEmpty(channel_code)){
			sb.append(" and channel_code = ?");
			paramsList.add(channel_code);
		}
		if(iDisplayStart != null && iDisplayLength != null){
			String limit=" limit "+iDisplayStart+" , "+iDisplayLength;
			sb.append(limit);
		}
		try{
			beanList=dao.list(sb.toString(),Channel.class,paramsList);
		}catch(AppException e){
			e.printStackTrace();
		}
		return beanList;
	}

	/**
	 * 查询渠道信息的记录数的方法
	 */
	@Override
	public int getProductCount(Map<String, Object> paramsMap) {
        BaseDao dao=new BaseDao(Constant.POOL_LIFE);
		List<Object> paramsList=new ArrayList<Object>();
		StringBuffer sb=new StringBuffer();
		sb.append("select count(*) from comm_product_channel where 1=1");
		String name=(String) paramsMap.get("channel_name");
		if(name!=null && name!=""){
			sb.append(" and channel_name like ?");
			paramsList.add(name);
		}
		int i = 0;
		try{
			i=dao.getInt(sb.toString(), paramsList);
		}catch(AppException e){
			e.printStackTrace();
		}
		return i;
	}

	/**
	 * 新增渠道信息的方法
	 */
	@Override
	public boolean addChannel(Map<String, Object> paramsMap, Channel model) {
		String channel_name=(String) paramsMap.get("channel_name");
		String channel_code=(String) paramsMap.get("channel_code");
		String prdid=(String) paramsMap.get("prdid");
		StringBuffer sb=new StringBuffer();
		sb.append("insert comm_product_channel(channel_name,channel_code,prdid) values(?,?,?)");
		BaseDao dao=new BaseDao(Constant.POOL_LIFE);
		BaseDao testDao = new BaseDao(Constant.POOL_PUSH_RW);
		boolean result=true;
		try{
			List<Object> params=new ArrayList<Object>();
			params.add(channel_name);
			params.add(channel_code);
			params.add(prdid);
			dao.add(sb.toString(), params);
			testDao.add(sb.toString(),params);
		}catch(AppException e){
			e.printStackTrace();
			result=false;
		}
		return result;
	}
	
	/**
	 * 更新渠道信息的方法
	 */
	@Override
	public boolean updateChannel(Map<String, Object> paramsMap, Channel model,String id ){
		String channel_name=(String) paramsMap.get("channel_name");
		String channel_code=(String) paramsMap.get("channel_code");
		String prdid =(String) paramsMap.get("prdid");
		StringBuffer sb=new StringBuffer();
		sb.append("update comm_product_channel set channel_name = ?,channel_code = ?,prdid = ? where id = ? ");
		BaseDao dao=new BaseDao(Constant.POOL_LIFE);
		BaseDao testDao = new BaseDao(Constant.POOL_PUSH_RW);
		boolean result=true;
		try{
			List<Object> params=new ArrayList<Object>();
			params.add(channel_name);
			params.add(channel_code);
			params.add(prdid);
			params.add(id);
			dao.add(sb.toString(), params);
			testDao.add(sb.toString(),params);
		}catch(AppException e){
			e.printStackTrace();
			result=false;
		}
		return result;
	}
	
	/**
	 * 删除渠道的方法
	 */
	@Override
	public boolean dropChannel(String id) {
		boolean result=true;
		BaseDao dao=new BaseDao(Constant.POOL_LIFE);
		StringBuffer sb=new StringBuffer();
		sb.append("delete from treasure_channel where id=?");
		List<Object> params=new ArrayList<Object>();
		params.add(id);
		try{
			dao.delete(sb.toString(), params);
		}catch(AppException e){
			e.printStackTrace();
			result=false;
		}
		return result;
	}
}
